/// 用户数据模型
/// 
/// 用户信息的实体类
/// 继承BaseModel，实现JSON序列化
/// 
/// 作者: Fluttert Team
/// 日期: 2024

import 'base_model.dart';

/// 用户模型类
/// 
/// 包含用户的基本信息
class UserModel extends BaseModel {
  /// 用户ID
  final int id;
  
  /// 用户名
  final String name;
  
  /// 用户邮箱
  final String email;
  
  /// 用户头像URL
  final String? avatar;
  
  /// 用户年龄
  final int? age;
  
  /// 创建时间
  final DateTime? createdAt;

  /// 构造函数
  UserModel({
    required this.id,
    required this.name,
    required this.email,
    this.avatar,
    this.age,
    this.createdAt,
  });

  /// 从JSON创建UserModel实例（静态方法）
  /// 
  /// [json] JSON数据Map
  /// 返回UserModel实例
  static UserModel fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'] as int,
      name: json['name'] as String,
      email: json['email'] as String,
      avatar: json['avatar'] as String?,
      age: json['age'] as int?,
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'] as String)
          : null,
    );
  }

  /// 从JSON创建UserModel实例（实现BaseModel接口）
  @override
  BaseModel fromJson(Map<String, dynamic> json) {
    return UserModel.fromJson(json);
  }

  /// 将UserModel转换为JSON格式
  /// 
  /// 返回包含用户数据的Map
  @override
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'email': email,
      'avatar': avatar,
      'age': age,
      'createdAt': createdAt?.toIso8601String(),
    };
  }

  /// 创建UserModel的副本，可选择性地修改字段
  /// 
  /// 返回新的UserModel实例
  UserModel copyWith({
    int? id,
    String? name,
    String? email,
    String? avatar,
    int? age,
    DateTime? createdAt,
  }) {
    return UserModel(
      id: id ?? this.id,
      name: name ?? this.name,
      email: email ?? this.email,
      avatar: avatar ?? this.avatar,
      age: age ?? this.age,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  String toString() {
    return 'UserModel(id: $id, name: $name, email: $email, age: $age)';
  }
}

